package com.etoak.service.impl;


import com.etoak.bean.*;
import com.etoak.mapper.HomestayMapper;
import com.etoak.mapper.HomestayOrderMapper;
import com.etoak.mapper.RoomTypeMapper;
import com.etoak.service.OrderService;
import com.etoak.utils.OrderNumUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    HomestayMapper homestayMapper;

    @Resource
    RoomTypeMapper typeMapper;

    @Resource
    HomestayOrderMapper mapper;

    @Override
    public int addOrder(HomestayOrder order) {
        HomestayVO homestayVO = homestayMapper.queryHomestayRoomsById(order);
        if(homestayVO.getRooms()>=order.getRooms()){
            order.setOnum(OrderNumUtil.getOrderNum());
            order.setStatus(1);
            return mapper.insert(order);
        }
        return -1;
    }

    @Override
    public Page selectByUid(Page page, String uid) {
        PageHelper.startPage(page.getPageNum(),page.getPageSize());
        List<HomestayOrderVO> orders = mapper.selectByUid(uid);
        PageInfo<HomestayOrderVO> pageInfo = new PageInfo<>(orders);
        page.setRows(orders);
        page.setTotal(pageInfo.getTotal());
        return page;
    }

    @Override
    public HomestayOrderVO selectByOnum(String onum) {
        return mapper.selectByOnum(onum);
    }

    @Override
    public int updateStatus(String onum,Integer status) {
        return mapper.updateStatus(onum,status);
    }

    @Override
    public int del(String onum) {
        return mapper.del(onum);
    }


}
